package com.unknowpupil.alibaba.cloud.dto;

/**
 *  结果编码集。
 * @Classname: ResultCode
 * @Author: <a> w10g </a>
 * @CreateDate: Create in 2020/1/16 13:26
 * @Description:
 * * #200表示成功
 *  * #10001～19999 区间表示参数错误
 *  * #20001～29999 区间表示用户错误
 *  * #30001～39999 区间表示接口异常
 *  * #40001～49999 区间表示自定义异常
 * @Version: 1.0
 */
public enum ErrorState {

    /* 成功 */
    SUCCESS(200, "成功"),

    /* 默认失败 */
    COMMON_FAIL(999, "失败"),

    /* 参数错误：1000～1999 */

    PARAM_IS_INVALID(10001, "参数无效"),
    PARAM_IS_BLANK(10002, "参数为空"),
    PARAM_TYPE_BIND_ERROR(10003, "参数类型错误"),
    PARAM_NOT_COMPLETE(10004, "参数缺失"),
    PARAM_IS_ERROR(10005, "参数缺失"),
    PARAM_IS_NULL(10005, "空指针异常"),

    /* 用户错误 */

    USER_NOT_LOGIN(20001, "用户未登录"),
    USER_NOT_NULL(20013, "账号不能为空"),
    USER_ACCOUNT_EXPIRED(20002, "账号已过期"),
    USER_CREDENTIALS_ERROR(20003, "密码错误"),
    USER_CREDENTIALS_EXPIRED(20004, "密码过期"),
    USER_ACCOUNT_DISABLE(20005, "账号不可用"),
    USER_ACCOUNT_LOCKED(20006, "账号被锁定"),
    USER_ACCOUNT_NOT_EXIST(20007, "账号不存在"),
    USER_ACCOUNT_ALREADY_EXIST(20008, "账号已存在"),
    USER_ACCOUNT_USE_BY_OTHERS(20009, "账号下线"),
    NO_AOUTH_ACCESS(20010, "账号下线"),
    USER_PASSWORD_FAILURE(20011, "密码生成错误"),

    /* 业务错误 */
    NO_PERMISSION(60001, "没有权限"),


    /*自定义异常 */
    NOT_HAVE_ENUM_ERROR(40001,"没有此类枚举"),
    ACCESS_FAILURE(40002,"接口访问失败"),

    /*** 系统错误：40001-49999 */
    SYSTEM_INNER_ERROR(40001, "系统繁忙，请稍后重试"),

    /*** 数据错误：50001-599999 */
    RESULT_DATA_NONE(50001, "数据未找到"),
    DATA_IS_WRONG(50002, "数据有误"),
    DATA_ALREADY_EXISTED(50003, "数据已存在"),

    /* 接口错误：60001-69999 */
    INTERFACE_INNER_INVOKE_ERROR(60001, "内部系统接口调用异常"),
    INTERFACE_OUTTER_INVOKE_ERROR(60002, "外部系统接口调用异常"),
    INTERFACE_FORBID_VISIT(60003, "该接口禁止访问"),
    INTERFACE_ADDRESS_INVALID(60004, "接口地址无效"),
    INTERFACE_REQUEST_TIMEOUT(60005, "接口请求超时"),
    INTERFACE_EXCEED_LOAD(60006, "接口负载过高");

    private int code;
    private String message;

    ErrorState(int code, String msg) {
        this.code = code;
        this.message = msg;
    }

    public int getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public boolean success() {
        return this.code == 200;
    }

    public static ErrorState valueOf(int code) {
        for (ErrorState value : values()) {
            if (code == value.code) {
                return value;
            }
        }
        return SUCCESS;
    }

    /**
     * 通过code返回对应的错误信息
     * @param code
     * @return
     */
    public static String getMsg(Integer code){
        for(ErrorState apiCodeEnum: ErrorState.values()){
            if(code.equals(apiCodeEnum.getCode())){
                return apiCodeEnum.getMessage();
            }
        }
        return NOT_HAVE_ENUM_ERROR.getMessage();
    }


}
